//
//  SHLayoutViewController.m
//  SDemo
//
//  Created by gg on 2018/7/19.
//  Copyright © 2018年 gg. All rights reserved.
//

#import "SHLayoutViewController.h"

@interface SHLayoutViewController ()

@property(nonatomic,strong)UILabel *leftlabel;
@property(nonatomic,strong)UILabel *rightlabel;
@property(nonatomic,assign) NSInteger count;
@property(nonatomic,copy) NSString *times;


@property(nonatomic,strong)UIView *leftView;
@property(nonatomic,strong)UIView *rightView;

@end

@implementation SHLayoutViewController

-(UILabel *)leftlabel{
    if (_leftlabel == nil) {
        _leftlabel = [[UILabel alloc] init];
        _leftlabel.text = @"我是00000";
        _leftlabel.backgroundColor = [UIColor redColor];
    }
    
    return  _leftlabel;
}


-(UILabel *)rightlabel{
    if (_rightlabel == nil) {
        _rightlabel = [[UILabel alloc] init];
        _rightlabel.text = @"我是鹅000202020202020200000232323";
        _rightlabel.textAlignment = NSTextAlignmentRight;
        _rightlabel.backgroundColor = [UIColor redColor];
    }
    return  _rightlabel;
}

-(UIView *)leftView {
    if (_leftView == nil) {
        _leftView = [UIView new];
        _leftView.backgroundColor = [UIColor greenColor];
    }
    return _leftView;
}


-(UIView *)rightView {
    if (_rightView == nil) {
        _rightView = [UIView new];
        _rightView.backgroundColor = [UIColor cyanColor];
    }
    return _rightView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"布局设计";
    self.view.backgroundColor = [UIColor whiteColor];
    self.count = 1;
    self.times = @"听";
    [self.view addSubview:self.leftlabel];
    [self.view addSubview:self.rightlabel];
    
    
    __weak typeof (self) weakSelf = self;
    [self.leftlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(120);
        make.right.mas_lessThanOrEqualTo(self.rightlabel.mas_left).offset(-5).priorityLow();
        
    }];
    
    [self.rightlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.leftlabel);
        make.right.mas_equalTo(-20);
        //这个约束设置在低优先级的控件上
        make.left.mas_lessThanOrEqualTo(self.leftlabel.mas_right).offset(5);
    }];
    
    //设置优先级  低优先级的是想要显示完全的内容 UILayoutPriorityFittingSizeLevel //可以被压缩
    [self.leftlabel setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)]; //不可以被压缩
    [self.rightlabel setContentCompressionResistancePriority:(UILayoutPriorityDefaultLow) forAxis:(UILayoutConstraintAxisHorizontal)];
    
//    [self.leftlabel setContentHuggingPriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
//    [self.rightlabel setContentHuggingPriority:(UILayoutPriorityDefaultLow) forAxis:(UILayoutConstraintAxisHorizontal)];
//
    
    UIButton *letfbutton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [letfbutton setBackgroundColor:[UIColor blueColor]];
    [letfbutton setTitle:@"改变左边宽" forState:UIControlStateNormal];
    letfbutton.tag = 11;
    [letfbutton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:letfbutton];
    
    UIButton *rightButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [rightButton setBackgroundColor:[UIColor blueColor]];
    [rightButton setTitle:@"改变右边宽" forState:UIControlStateNormal];
    rightButton.tag = 12;
    [rightButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightButton];
    
    
    [letfbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.width.equalTo(@100);
        make.height.equalTo(@45);
        make.top.equalTo(self.leftlabel.mas_bottom).offset(30);
    }];
    
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(letfbutton);
        make.right.equalTo(@(-20));
    }];
    
    [self.view addSubview:self.leftView];
    [self.view addSubview:self.rightView];
    
    [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(letfbutton.mas_bottom).offset(20);
        make.height.width.with.mas_equalTo(100);

    }];
    
//    [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.left.top.mas_equalTo(weakSelf.leftView);
//        make.right.mas_equalTo(weakSelf.leftView.mas_left).offset(10);
//        make.width.mas_equalTo(weakSelf.leftView).multipliedBy(2);
//    }];
    
    
}

-(void)buttonClick:(id)sender {
    UIButton *button = (UIButton *)sender;
    self.count += 1;
    self.times = [NSString stringWithFormat:@"听%@",self.times];
    
    if (button.tag == 11) {
        self.leftlabel.text = [NSString stringWithFormat:@"我是左边的视图%@",self.times];
    } else {
        self.rightlabel.text = [NSString stringWithFormat:@"我是右边的视图%@",self.times];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
